package com.stylefeng.guns.modular.system.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.persistence.dao.DictMapper;
import com.stylefeng.guns.common.persistence.model.Dict;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.modular.system.bean.DictReq;
import com.stylefeng.guns.modular.system.service.IDictToCService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lyz on 2018/2/4.
 */
@Service
public class DictToCServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictToCService {

    @Autowired
    private ConstantFactory constantFactory;

    @Override
    public List<Map<String,Object>> getDictByPid(DictReq req){

        if(req == null || req.getId() == null){
            throw new GunsException(BizExceptionEnum.PARAM_ERROR);
        }
        List<Dict> dictList = constantFactory.findDictByPID(req.getId());
        if(CollectionUtils.isEmpty(dictList)){
            return null;
        }
        List<Map<String,Object>> result = new ArrayList<>();
        for (Dict dict : dictList){
            Map<String,Object> temp = new HashMap<>(2,1);
            temp.put("id",dict.getId());
            temp.put("name",dict.getName());
            result.add(temp);
        }
        return result;
    }

    @Override
    public String getDictById(DictReq req) {
        return constantFactory.findDictTips(req.getId());
    }

    @Override
    public String getDictByName(DictReq req) {
        return constantFactory.findDictByName(req.getName());
    }
}
